package com.teufandsub.socialproximity.gui;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.teufandsub.socialproximity.R;
import com.teufandsub.socialproximity.controller.Constants;
import com.teufandsub.socialproximity.controller.Controller;
import com.teufandsub.socialproximity.logic.Friend;
import com.teufandsub.socialproximity.logic.User;

/**
 * @author Bastien Mathias <bastienmathias@gmail.com>
 * @author Antoine Teuf <antoine.teuf@gmail.com>
 * @version 1.0
 * @since 2013-05-22
 * 
 * The adapter class to fill up the view after a research.
 */

public class FriendResultAdapter extends ArrayAdapter<Friend>{

	private ArrayList<Friend> friends;
	private Context context;
	private Controller controller;
	
	/**
	 * Constructor of the Adapter
	 * 
	 * @param context Context of the application.
	 * @param friends ArrayList<Friend> list of friend to display.
	 */
	public FriendResultAdapter(Context context, ArrayList<Friend> friends){
		super(context,0,friends);
		this.friends = friends;
		this.context = context;
		controller = Controller.getInstance();
		
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.user_list_layout, parent, false);
        final Friend friend = friends.get(position);
             
        try {
        	TextView username = (TextView) convertView.findViewById(R.id.username_list);
        	TextView matchingPercent = (TextView) convertView.findViewById(R.id.matching_percentage_list);
        	TextView distance = (TextView) convertView.findViewById(R.id.distance_list);
        	
        	/*Set user name view */
        	if(friend.showRealName()){
        		username.setText(friend.getFirstname()+" "+friend.getSurname());        		
        	}else{
        		username.setText(friend.getUsername());
        	}
	    	switch(friend.getSex()){
				case User.SEX_MALE: username.setTextColor(Color.BLUE);break;
				case User.SEX_FEMALE: username.setTextColor(Color.MAGENTA);break;
			}
	    	
	    	/*Set matching percent view*/
	    	matchingPercent.setText(friend.getMatchingPercent()+"%");
	    	
	    	/*Set distance view*/
	    	if(friend.getDistance()>1000){
	    		distance.setText(String.format("%.2fkm", friend.getDistance()/1000));
	    	}else{
	    		distance.setText(String.format("%.0fm", friend.getDistance()));
	    	}
	    	
	    	ImageButton chatButton = (ImageButton) convertView.findViewById(R.id.chat_button);
	    	chatButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent i = new Intent(context, ChattingActivity.class);
					i.putExtra(User.KIND, friend);
					if(controller.isNewConversation(friend)){
						controller.newConversation(friend);
					}
					
					context.startActivity(i);
				}
			});
	    	
		} catch (Exception e) {
			Log.e(Constants.APP_NAME, "FriendResulAdapter Error : "+e.toString());
		}
        
        return convertView;
	}
}
